package com.example.mtblog.common;

import com.example.mtblog.dto.*;
import com.example.mtblog.dto.UserAuthority;
import com.example.mtblog.dto.UserPrivacy;
import com.example.mtblog.tool.RedisUtils;
import com.example.mtblog.tool.StrUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class common {

     public static   String   userInfoToken = "UserInfo:";
     public static   String   userAuthorityToken = "UserAuthority:";

      public static   String   userPrivacyToken = "UserPrivacy:";
      public static   String   articleInfoToken = "ArticleInfo:";

     /**
     * @description: 获取用户权限
     * @author MT
     * @date 2024/3/14 9:35
     */

    public  static UserAuthority getUserAuthority(Integer uid){
        UserAuthority userAuthority = new UserAuthority();
        try{
            if(RedisUtils.getCacheObject(userAuthorityToken + uid) != null)
                userAuthority = RedisUtils.getCacheObject(userAuthorityToken + uid) ;
           else {
                userAuthority.init(userAuthority);
                userAuthority.setUid(uid);
                RedisUtils.setCacheObject(userAuthorityToken + uid,userAuthority);
            }
        }catch (Exception e){
            log.error(e + " \n error in  common/getUserAuthority");
            return userAuthority;
        }

        return userAuthority;
    }

    /***
    * @description: 获取用户信息
    * @author MT
    * @date 2024/4/11 19:03
    */
    public static UserInfo getUserInfo(Integer uid)
    {
        UserInfo userInfo = new UserInfo();


        try{
            if(RedisUtils.getCacheObject(userInfoToken + uid) != null)
                userInfo = RedisUtils.getCacheObject(userInfoToken + uid) ;
            else {
                userInfo.init(userInfo);
                userInfo.setUid(uid);
                RedisUtils.setCacheObject(userInfoToken + uid,userInfo);
            }
        }catch (Exception e){
            log.error(e + " \n error in  common/getUserInfo");
            return userInfo;
        }

        return userInfo;

    }

    /***
     * @description: 获取用户隐私
     * @author MT
     * @date 2024/4/11 19:03
     */
    public static UserPrivacy getUserPrivacy(Integer uid)
    {
        UserPrivacy userPrivacy = new UserPrivacy();


        try{
            if(RedisUtils.getCacheObject(userPrivacyToken + uid) != null)
                userPrivacy = RedisUtils.getCacheObject(userPrivacyToken + uid) ;
            else {
                userPrivacy.init(userPrivacy);
                userPrivacy.setUid(uid);
                RedisUtils.setCacheObject(userPrivacyToken + uid,userPrivacy);
            }
        }catch (Exception e){
            log.error(e + " \n error in  common/getUserPrivacy");
            return userPrivacy;
        }

        return userPrivacy;

    }

    /***
     * @description: 获取文章信息
     * @author MT
     * @date 2024/4/11 19:03
     */
    public static ArticleInfo getArticleInfo(Integer id )
    {
        ArticleInfo articleInfo = new ArticleInfo();

        try{
            if(RedisUtils.getCacheObject(articleInfoToken + id) != null)
                articleInfo = RedisUtils.getCacheObject(articleInfoToken + id) ;
            else {
                articleInfo.init(articleInfo);
                articleInfo.setId(id);
                RedisUtils.setCacheObject(articleInfoToken + id,articleInfo);
            }
        }catch (Exception e){
            log.error(e + " \n error in  common/getArticleInfo");
            return articleInfo;
        }

        return articleInfo;

    }

    public  static void setUserAuthority(UserAuthority userAuthority){
        try{
            RedisUtils.setCacheObject(userAuthorityToken + userAuthority.getUid(), userAuthority);
        }catch (Exception e){
            log.error(e + " \n error in  common/setUserAuthority");
        }

    }

    public  static void setUserInfo(UserInfo userInfo){
        try{
            RedisUtils.setCacheObject(userInfoToken + userInfo.getUid(), userInfo);
        }catch (Exception e){
            log.error(e + " \n error in  common/setUserInfo");
        }

    }


    public  static void setArticleInfo(ArticleInfo articleInfo){
        try{
            RedisUtils.setCacheObject(articleInfoToken + articleInfo.getId(), articleInfo);
        }catch (Exception e){
            log.error(e + " \n error in  common/setArticleInfo");
        }

    }

    public  static void setUserPrivacy(UserPrivacy userPrivacy){
        try{
            RedisUtils.setCacheObject(userPrivacyToken + userPrivacy.getUid(), userPrivacy);
        }catch (Exception e){
            log.error(e + " \n error in  common/setUserPrivacy");
        }

    }


    public static  Integer getLoginUid(HttpServletRequest request){
        LoginUser loginUser = (LoginUser) request.getAttribute("loginUser");
        if(loginUser == null)
            return  -1;
        return  loginUser.getUid();
    }


    public  static Boolean isEmpty(String value){
        return value == null || value.isEmpty();
    }

    public  static Boolean isEmpty(Integer value){
        if(value ==null)
            return  true;

        String str = value.toString();
        return str.isEmpty();
    }

    public  static Integer getNum(Integer num){
        if(num == null)
            return  0;
        return  num;
    }

    public  static Integer add(Integer value ,Integer addNum){
        if(isEmpty(value))
            return  addNum;
        return value + addNum;
    }

    public  static  String getSqlIn(String value){

        if(isEmpty(value))
            return  "('-1')";

        StringBuffer stringBuffer= StrUtils.changeToIn(value);
        if(value == null)
            return  "('-1')";

        return "(" + stringBuffer.substring(0, stringBuffer.length()-1) + ")";
    }

    public  static  String getOrderBySql(String table, String type,String value){

        String common = "ORDER BY Field (" + table + "." + type +",";
        if(isEmpty(value))
            return  common + "'-1')";

        StringBuffer stringBuffer= StrUtils.changeToIn(value);
        if(value == null)
            return  common + "'-1')";

        return  common + stringBuffer.substring(0, stringBuffer.length()-1) + ")";
    }

}


